//
//  TSProfitTableViewCell.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/15.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

class TSProfitTableViewCell: UITableViewCell {
    
    private var coinItem : TSProfitItemView!
    private var pointItem : TSProfitItemView!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func refreshCell() {
        
        let coin = NSString.init(string: "\(TSLoginManager.shared._usermodel!.jinbi)")
        coinItem.valueLabel.text = "\(coin.integerValue)"
        
        pointItem.valueLabel.text = String.init(format: "%.2f", TSLoginManager.shared._usermodel!.liushui_now)
    }
    
    private func configViews() {
                
        let coin = NSString.init(string: "\(TSLoginManager.shared._usermodel!.jinbi)")
        coinItem = TSProfitItemView.init("金币", value:"\(coin.integerValue)")
        contentView.addSubview(coinItem)
        pointItem = TSProfitItemView.init("流水", value: String.init(format: "%.2f", TSLoginManager.shared._usermodel!.liushui_now))
        contentView.addSubview(pointItem)
        let itemArray = [coinItem, pointItem]
        itemArray.snp.distributeViewsAlong(axisType: .horizontal, fixedItemLength: SCREEN_WIDTH*0.5, leadSpacing: 0, tailSpacing: 0)
        itemArray.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.height.equalTo(60)
            
            make.bottom.equalToSuperview()
        }
        
        let line = UIView.init()
        line.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#E6E6E6")
        contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.centerX.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: 1, height: 32))
        }
    }
}

class TSProfitItemView : UIView {
    
    private var titleLabel : UILabel!
    var valueLabel : UILabel!
    
    convenience init(_ title: String, value:String) {
        self.init()
        configViews(title, value: value)
    }
    
    private func configViews(_ title : String, value:String) {
        
        titleLabel = UILabel.init()
        titleLabel.text = title
        titleLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#646464")
        titleLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(14)
        }
        
        valueLabel = UILabel.init()
        valueLabel.text = value
        valueLabel.textAlignment = .center
        valueLabel.textColor = UIColor.hexadecimalColor(hexadecimal: "#646464")
        valueLabel.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        valueLabel.adjustsFontSizeToFitWidth = true
        valueLabel.minimumScaleFactor = 0.6
        self.addSubview(valueLabel)
        valueLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp_bottom).offset(6)
            make.left.right.equalToSuperview()
        }
    }
}
